package com.aizhixin.test.question.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.aizhixin.test.question.dto.AllAnswerRecordDTO;
import com.aizhixin.test.question.dto.anserEverydayDTO;
import com.aizhixin.test.question.dto.answerRecordDTO;
import com.aizhixin.test.question.dto.judgeQuestionNeedDTO;

import aj.org.objectweb.asm.Type;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class judgeDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/*
	 * stamid查找t_question里面的anser和konledge存入judgeDTO里面
	 * SELECT q.ANSWER,q.KNOWLEDGE_AREA FROM t_question as q where q.ID=?
	 */
	
	public judgeQuestionNeedDTO getRightAnserAndKonledge(Long userId,Long stamId,String UserAnser) {
		final String sql="SELECT q.ANSWER,q.KNOWLEDGE_AREA FROM t_question as q where q.ID=?";
		List<judgeQuestionNeedDTO> list =jdbcTemplate.query(sql, 
				new Object []{stamId}, 
				new int[] {Types.INTEGER},
				new RowMapper<judgeQuestionNeedDTO>() {
					public judgeQuestionNeedDTO mapRow(ResultSet rs,
							int rowNum)
					throws SQLException{
				judgeQuestionNeedDTO test=new judgeQuestionNeedDTO();
						test.setRightAnser(rs.getString("ANSWER"));
						test.setKnoledge(rs.getString("KNOWLEDGE_AREA"));
						test.setSteamId(stamId);
						test.setUserAnser(UserAnser);
						test.setUserid(userId);
						return test;
					}
				});
		
		if (null != list && list.size() > 0) {
			return list.get(0);
		}else {
			return null;
		}
	}
	/*
	 * anser和rightanser相等情况，存入judge的DTO里面同时将分值point存进去ifElse
	 */
	public judgeQuestionNeedDTO setRightWayAddPointAndRightOrNot(judgeQuestionNeedDTO dto) {
		dto.setPoint(3);//设置本题得分
		dto.setRightOrNot(1);//对=1，错=0
		return dto;
	}
	/*
	 * 不等情况
	 */
	public judgeQuestionNeedDTO setErrolWay(judgeQuestionNeedDTO dto) {
		dto.setPoint(-1);
		dto.setRightOrNot(0);
		return dto;
	}
		/*
		 * 增数据answer_record
		 * INSERT INTO answer_record (
		 * STEM_ID,
		 * USER_ANSWER,
		 * USWER_THIS_STEM_RIGHT_OR_NOT,
		 * USER_ID,
		 * RIGHT_ANSWR,
		 * ASWER_TIME,
		 * KNOWLEDGE,
		 * THIS_STEM_POINTS) VALUES (?,?,?,?,?,NOW(),?,?);

		 */
		public judgeQuestionNeedDTO insertAnserdto(judgeQuestionNeedDTO dto) {
			final String sql="INSERT INTO answer_record (STEM_ID,USER_ANSWER,USWER_THIS_STEM_RIGHT_OR_NOT,USER_ID,RIGHT_ANSWR,ASWER_TIME,KNOWLEDGE,THIS_STEM_POINTS) VALUES (?,?,?,?,?,NOW(),?,?);";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1, dto.getSteamId());
				ps.setString(2, dto.getUserAnser());
				ps.setInt(3, dto.getRightOrNot());
				ps.setLong(4, dto.getUserid());
				ps.setString(5, dto.getRightAnser());
				ps.setString(6, dto.getKnoledge());
				ps.setLong(7, dto.getPoint());
				return ps;
			});
			return dto;
		}
		/*
		 * SELECT q.NOW_POINTS FROM answer_points_president_accounts as q WHERE q.USER_ID=?
		 */
		public Long cheackcanDebuffOrNot(Long userId) {
			final String sql="SELECT q.NOW_POINTS FROM answer_points_president_accounts as q WHERE q.USER_ID=? ";
			List<AllAnswerRecordDTO> list =jdbcTemplate.query(sql, 
					new Object[] {userId},
					new int[] {Type.LONG},
					new RowMapper<AllAnswerRecordDTO>() {
						public AllAnswerRecordDTO mapRow(ResultSet rs,
								int rowNum)
						throws SQLException{
							AllAnswerRecordDTO test=new AllAnswerRecordDTO();
							test.setNowPiont(rs.getLong("NOW_POINTS"));
						return test;
						}
						});
			if(list.size() > 0) {
				return list.get(0).getNowPiont();
			}
			return 0L;
		}
	public int  addAllRecord(judgeQuestionNeedDTO dto) {
		Long a=cheackcanDebuffOrNot(dto.getUserid());//检查结果是否》=1（经得起扣）
			if(dto.getRightOrNot()==1) {
			/*INSERT INTO answer_points_president_accounts
			 * (USER_ID,
			 * NOW_POINTS,
			 * RIGHT_STEM_TOTAL_NUMBERS,
			 * ERROL_STEM_TOTAL_NUMBERS,
			 * TOTAL_ANSWER_NUMBERS)
			 * VALUES(?,?,?,?,1)
			 */

			final String sql="INSERT INTO answer_points_president_accounts(USER_ID,NOW_POINTS,RIGHT_STEM_TOTAL_NUMBERS,ERROL_STEM_TOTAL_NUMBERS,TOTAL_ANSWER_NUMBERS)VALUES(?,?,?,?,1)";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1, dto.getUserid());
				ps.setInt(2, dto.getPoint());
				ps.setLong(3, 1);
				ps.setLong(4, 0);
				
				return ps;
			});
		}
		
		else if (a>=1) {
			final String sql="INSERT INTO answer_points_president_accounts(USER_ID,NOW_POINTS,RIGHT_STEM_TOTAL_NUMBERS,ERROL_STEM_TOTAL_NUMBERS,TOTAL_ANSWER_NUMBERS)VALUES(?,?,?,?,1)";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1, dto.getUserid());
				ps.setInt(2, dto.getPoint());
				ps.setLong(3, 0);
				ps.setLong(4, 1);
				return ps;
			});
		}
		else {
			final String sql="INSERT INTO answer_points_president_accounts(USER_ID,NOW_POINTS,RIGHT_STEM_TOTAL_NUMBERS,ERROL_STEM_TOTAL_NUMBERS,TOTAL_ANSWER_NUMBERS)VALUES(?,?,?,?,1)";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1, dto.getUserid());
				ps.setInt(2, 0);
				ps.setLong(3, 0);
				ps.setLong(4, 1);
				return ps;
			});
		}
	return 200;
	}
	/*
	 * UPDATE answer_points_president_accounts SET 
	 * NOW_POINTS=NOW_POINTS+?,
	 * RIGHT_STEM_TOTAL_NUMBERS=RIGHT_STEM_TOTAL_NUMBERS+?,
	 * ERROL_STEM_TOTAL_NUMBERS=ERROL_STEM_TOTAL_NUMBERS+?,
	 * TOTAL_ANSWER_NUMBERS=TOTAL_ANSWER_NUMBERS+1 
	 * WHERE USER_ID=?

	 */
	public int changeAllRecord(judgeQuestionNeedDTO dto) {
		Long a=cheackcanDebuffOrNot(dto.getUserid());//检查结果是否》=1（经得起扣）
		if(dto.getRightOrNot()==1) {//如果是对的
			final String sql="UPDATE answer_points_president_accounts SET NOW_POINTS=NOW_POINTS+?,RIGHT_STEM_TOTAL_NUMBERS=RIGHT_STEM_TOTAL_NUMBERS+?,ERROL_STEM_TOTAL_NUMBERS=ERROL_STEM_TOTAL_NUMBERS+?,TOTAL_ANSWER_NUMBERS=TOTAL_ANSWER_NUMBERS+1 WHERE USER_ID=?";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1,3);
				ps.setInt(2,1 );
				ps.setLong(3, 0);
				ps.setLong(4, dto.getUserid());
				
				return ps;
			});
		}
		else if (a>=1) {
			final String sql="UPDATE answer_points_president_accounts SET NOW_POINTS=NOW_POINTS+?,RIGHT_STEM_TOTAL_NUMBERS=RIGHT_STEM_TOTAL_NUMBERS+?,ERROL_STEM_TOTAL_NUMBERS=ERROL_STEM_TOTAL_NUMBERS+?,TOTAL_ANSWER_NUMBERS=TOTAL_ANSWER_NUMBERS+1 WHERE USER_ID=?";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1,-1);
				ps.setInt(2,0 );
				ps.setLong(3, 1);
				ps.setLong(4, dto.getUserid());
				
				return ps;
			});
		}
		else {
			final String sql="UPDATE answer_points_president_accounts SET NOW_POINTS=NOW_POINTS+?,RIGHT_STEM_TOTAL_NUMBERS=RIGHT_STEM_TOTAL_NUMBERS+?,ERROL_STEM_TOTAL_NUMBERS=ERROL_STEM_TOTAL_NUMBERS+?,TOTAL_ANSWER_NUMBERS=TOTAL_ANSWER_NUMBERS+1 WHERE USER_ID=?";
			jdbcTemplate.update(ConcatExpression ->{
				PreparedStatement ps =ConcatExpression.prepareStatement(sql);
				ps.setLong(1,0);
				ps.setInt(2,0 );
				ps.setLong(3, 1);
				ps.setLong(4, dto.getUserid());
				
				return ps;
			});
		}
		
		return 200;
	}
	
	
}
